//
//  MainViewController.swift
//  weiBo
//
//  Created by mac on 15/11/22.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        setupComposedButton()
    }
    
    //MARK: -懒加载控件
    private lazy var composedButton: UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
}


// MARK: -设置界面
extension MainViewController {
    
    ///设置撰写按钮
    private func setupComposedButton() {
        //添加按钮
        tabBar.addSubview(composedButton)
        //调整按钮
        let count = childViewControllers.count
        //按钮的宽度
        //让按钮宽一点点,可以解决按钮的容错问题
        let w = tabBar.bounds.size.width / CGFloat(count) - 1
        
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        
    }
    
    //添加所有的子控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    ///添加控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        
        
        //设置标题
        vc.title = title
        
        //设置图像
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
}